package Serverimpl;

import java.io.*;
import java.net.Socket;

public class SimpleClient {


    public static void main(String[] args){
        String ip_addres = "127.0.0.1";
        int port_number = 12345;

        try {
            Socket socket = new Socket(ip_addres, port_number);
            System.out.println("Coonect to server. Ok");


            OutputStream os = socket.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);

            InputStream is = socket.getInputStream();
            System.out.println("Input Stream recieved");
            ObjectInputStream ois = new ObjectInputStream(is);
            System.out.println("Object Input Stream recieved");

            SimpleClientReader clientReader = new SimpleClientReader(ois);
            clientReader.setDaemon(true);
            clientReader.start();
            System.out.println("Ready for reading. Starting writing");
            oos.writeObject("Hello Slon");
            oos.writeObject("Go dota? ;)");
            oos.writeObject("Pick Lina!");

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                System.out.println("InterruptedException in client Sleeping");
            }
        } catch (IOException e) {
            System.out.println("IOException in client");
        }
    }
}
class SimpleClientReader extends Thread{
      private ObjectInputStream ois;

    SimpleClientReader(ObjectInputStream ois) {
        this.ois = ois;
    }
    public void run(){
        while(true){
            try {
                Object obj = ois.readObject();
                System.out.println("Server's responce: "+obj);
            } catch (IOException e) {
                System.out.println("IOException in client reader thread");
            } catch (ClassNotFoundException e) {
                System.out.println("CleassNotFoundExceptio in client reader thread");
            }
        }
    }
}
